using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.Serialization;

using Impact.Controllers;
using Impact.Infrastructure;
using Impact.Infrastructure.Exceptions;
using Impact.Model.Objects;
using Impact.Model.Service;
using Impact.Views;

using StructureMap;

namespace Impact.Web.Foundation.Handlers
{
    /// <summary>
    /// HttpHandler for retrieving RSS feeds
    /// </summary>
    public class RssHandler : IHttpHandler, IImpactHandler
    {

        #region IHttpHandler Members

        void IHttpHandler.ProcessRequest( HttpContext context )
        {
            string channelName = context.Request.QueryString["c"];
            string userName = context.Request.QueryString["u"];

            RssController controller = new RssController( this );

            RssFeed feed = controller.GetRssFeed( channelName, userName,
                    HttpContext.Current.Request.UserHostAddress );
            feed.Serialize( context.Response.OutputStream );
            context.Response.ContentType = "text/xml";
        }

        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }

        #endregion

        #region IImpactHandler Members

        /// <summary>
        /// resolves a relative URL into an absolute URL for this application
        /// </summary>
        public string ResolveRelativeUrl( string url )
        {
            return WebUtility.ToAbsolute( url ).ToString();
        }

        #endregion
    }
}